package com.app.service.impl;


import com.app.mapper.LivePortendMapper;
import com.app.model.CusEducationModel;
import com.app.model.LivePortendModel;
import com.app.model.TrainEnrollModel;
import com.app.service.CusEducationService;
import com.app.service.LivePortendService;
import com.app.util.CommonHelp;
import com.app.util.StringTool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @Description:直播预告
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class LivePortendServiceImpl implements LivePortendService {
    @Autowired
    LivePortendMapper livePortendMapper;


    @Override
    public Page<LivePortendModel> modelPage(LivePortendModel model) {
        QueryWrapper<LivePortendModel> queryWrapper = new QueryWrapper<>();
        if (model != null) {
            if (model.getTitle() != null) {
                queryWrapper.like("title", model.getTitle());
            }
            if (model.getStatus() != null && model.getStatus() > 0) {
                queryWrapper.eq("status", model.getStatus());
            }
            if (StringTool.isNotEmpty(model.getStartTime())) {
                queryWrapper.ge("start_time", model.getStartTime());
            }
            if (StringTool.isNotEmpty(model.getEndTime())) {
                queryWrapper.le("start_time", model.getEndTime());
            }
        }
        queryWrapper.orderByDesc("crt_time");
        Page<LivePortendModel> page = new Page<>();
        page.setCurrent(model.getPage());
        page.setSize(model.getLimit());
        Page<LivePortendModel> modelPage = livePortendMapper.selectPage(page, queryWrapper);
        return modelPage;
    }

    @Override
    public int insert(LivePortendModel model) {
        return livePortendMapper.insert(model);
    }

    @Override
    public void update(LivePortendModel model) {
        livePortendMapper.updateById(model);
    }

    @Override
    public void delete(Integer id) {
        livePortendMapper.deleteById(id);
    }

    @Override
    public LivePortendModel getModelById(Integer id) {
        QueryWrapper<LivePortendModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return livePortendMapper.selectOne(queryWrapper);
    }

    @Override
    public List<LivePortendModel> getModelListByOnOpen() {
        QueryWrapper<LivePortendModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",2);
        String newTime = CommonHelp.getNowTimestamp().toString();
        queryWrapper.le("start_time",newTime);
        queryWrapper.ge("end_time", newTime);
        return livePortendMapper.selectList(queryWrapper);
    }

    @Override
    public List<LivePortendModel> getModelByUserId(Integer userId) {
        QueryWrapper<LivePortendModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return livePortendMapper.selectList(queryWrapper);
    }

    @Override
    public List<LivePortendModel> getModelListByOnClose() {
        QueryWrapper<LivePortendModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        String newTime = CommonHelp.getNowTimestamp().toString();
        queryWrapper.le("end_time", newTime);
        return livePortendMapper.selectList(queryWrapper);
    }

    @Override
    public LivePortendModel getModelListByModel(LivePortendModel model) {
        QueryWrapper<LivePortendModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1).eq("user_id",model.getUserId());
        if (StringTool.isNotEmpty(model.getStartTime())) {
            queryWrapper.le("start_time", model.getStartTime());
            queryWrapper.ge("end_time", model.getStartTime());
        }
        List<LivePortendModel> models = livePortendMapper.selectList(queryWrapper);
        if(models.isEmpty())
        {
            return null;
        }else {
            return models.get(0);
        }
    }
}
